﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using MySql.Data.MySqlClient;

/// <summary>
/// OleDB 的摘要说明
/// </summary>
public class OleDB
{
    private string dbPath;
    public string Conn;
  
    public OleDB()
    {
        getConnString();
    }

    public string getConnString()
    {
        this.Conn = "Database='unicom';Server='127.0.0.1';User Id='root';Password='820423';Port=3306;charset='utf8';pooling=true;Allow Zero Datetime=True";
        return Conn;
    }

    public DataSet getDataSet(string sql)
    {
        DataSet ds = MySqlHelper.GetDataSet(Conn, CommandType.Text, sql, null);
        return ds;
    }

    public bool executeSql(string sql)
    {
        bool flag = false;
        int i = MySqlHelper.ExecuteNonQuery(Conn, CommandType.Text, sql, null);
        if (i > 0)
        {
            flag = true;
        }
        return flag;
    }

    public object getValue(string query)
    {
        DataSet ds = getDataSet(query);
        return ds.Tables[0].Rows[0][0];
    }

   
    public int getIntegerValue(String sql)
    {
        int count = 0;
        MySqlDataReader reader = MySqlHelper.ExecuteReader(getConnString(),CommandType.Text,sql,null);
        try
        {
            if (reader.Read())
                count = reader.GetInt32(0);
        }
        catch (Exception)
        {
            return 0;
        }
        finally
        {
            reader.Close();
        }
        return count;
    }

    public String getStringValue(String sql)
    {
        String temp = "";
        MySqlDataReader reader = MySqlHelper.ExecuteReader(getConnString(), CommandType.Text, sql, null);
        try
        {
            if (reader.Read())
                temp = reader.GetString(0);
        }
        catch (Exception)
        {
            return "";
        }
        finally
        {
            reader.Close();
        }

        return temp;
    }

    public List<int> getIntegerList(String sql)
    {
        List<int> counts = new List<int>();
        MySqlDataReader reader = MySqlHelper.ExecuteReader(getConnString(), CommandType.Text, sql, null);
        try
        {
            while (reader.Read())
                counts.Add(reader.GetInt32(0));
        }
        catch (Exception)
        {
            return null;
        }
        finally
        {
            reader.Close();
        }

        return counts;
    }

    public List<String> getStringList(String sql)
    {
        List<String> temps = new List<String>();
        MySqlDataReader reader = MySqlHelper.ExecuteReader(getConnString(), CommandType.Text, sql, null);
        try
        {
            while (reader.Read())
                temps.Add(reader.GetString(0));
        }
        catch (Exception)
        {
            return null;
        }
        finally
        {
            reader.Close();
        }

        return temps;
    }

    public Object getDataValues(String sql)
    {
        Object obj = new Object();
        MySqlDataReader reader = MySqlHelper.ExecuteReader(getConnString(), CommandType.Text, sql, null);
        try
        {
            if (reader.Read())
                obj = reader.GetValue(0);
        }
        catch (Exception)
        {
            return null;
        }
        finally
        {
            reader.Close();
        }
        return obj;
    }
}
